package indi.imooc.datastructure.queue.impl;

/**
 * 双端队列
 * 循环队列,不浪费空间
 * 新增第一个元素时,front应当为data.length - 1位置的索引
 * size == capacity 是队列为满
 * @param <E>
 */
public class Deque<E>{

    private E[] data;
    private int front;
    private int tail;
    private int size;

    public Deque(int capacity){
        data = (E[]) new Object[capacity];
        front = 0;
        tail = 0;
        size = 0;
    }

    public Deque(){
        this(10);
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void addFront(E e) {
        if(size == getCapacity()){
            resize(2 * data.length);
        }
        // 注意新元素的位置
        front = front > 0 ? front - 1 : data.length - 1;
        // method 2
//        front = (front - 1 + data.length) % data.length;
        data[front] = e;
        size++;
    }

    public void addLast(E e) {
        if(size == getCapacity()){
            resize(2 * data.length);
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;
    }

    public E removeFront() {
        E e = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        if(size == data.length/4){
            resize(data.length/2);
        }
        return e;
    }

    public E removeLast() {
        if(isEmpty()){
            throw new IllegalArgumentException("Deque is empty");
        }
        tail = tail > 0 ? tail - 1 : data.length - 1;
        E e = data[tail];
        data[tail] = null;
        size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0){
            resize(data.length/2);
        }
        return e;
    }

    public E getFront() {
        if(isEmpty()){
            throw new IllegalArgumentException("Deque is empty.");
        }
        return data[front];
    }

    public E getLast(){
        if(isEmpty()){
            throw new IllegalArgumentException("Deque is empty.");
        }
        // 注意tail指向的是最后一个元素下一个位置的索引
        int index = tail > 0 ? tail - 1 : data.length - 1;
        return data[index];
    }

    private void resize(int capacity) {
        E[] newArr = (E[]) new Object[capacity];

        // method 1
//        for (int i = 0; i < size; i++) {
//            newArr[i] = data[(front + i) % data.length];
//        }

        // method 2
        for (int i = front,index = 0; index != size; i = (i + 1) % data.length) {
            newArr[index++] = data[i];
        }
        data = newArr;
        front = 0;
        tail = size;
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append(String.format("Deque : size = %d, capacity = %d\n",size, getCapacity()));
        res.append("front : [ ");
        // method 1
//        for (int i = 0; i < size; i++) {
//            res.append(data[(front + i) % data.length]);
//            if (i != size - 1){
//                res.append(";");
//            }
//        }
        // method 2
        for (int i = front,index = 0; index != size ; i = (i + 1) % data.length,index++) {
            res.append(data[i]);
            if (index != size - 1){
                res.append(", ");
            }
        }
        res.append(" ] tail");
        return res.toString();
    }

    public static void main(String[] args) {
//        Deque<Integer> deque = new Deque<>();
//        for (int i = 0; i < 16; i++) {
//            System.out.println(i);
//            deque.addFront(i);
//            System.out.println(deque.toString());
//            if (i % 3 == 2){
//                deque.removeFront();
//                System.out.println( deque.toString());
//            }
//        }
//
//        System.out.println(deque.toString());
//        for (int i = 0; i < 12; i++) {
//            deque.removeFront();
//            System.out.println(deque.toString());
//        }

        // 在下面的双端队列的测试中，偶数从队尾加入；奇数从队首加入
        Deque<Integer> dq = new Deque<>();
        for(int i = 0 ; i < 16 ; i ++){
            if(i % 2 == 0) dq.addLast(i);
            else dq.addFront(i);
            System.out.println(dq);
        }

        // 之后，我们依次从队首和队尾轮流删除元素
        System.out.println();
        for(int i = 0; !dq.isEmpty(); i ++){
            if(i % 2 == 0) dq.removeFront();
            else dq.removeLast();
            System.out.println(dq);
        }

    }

}
